package com.woniu.gym.controller;


import cn.hutool.core.lang.UUID;
import com.woniu.core.util.*;
import com.woniu.gym.dto.QualificationDto;
import com.woniu.gym.dto.RegistryGymDto;
import com.woniu.gym.form.*;
import com.woniu.gym.model.TQualification;
import com.woniu.gym.service.TQualificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.InputStream;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@RestController
@RequestMapping("/tQualification")
@Api(tags = "场馆入驻申请相关接口")
public class TQualificationController {
    @Resource
    private TQualificationService tQualificationService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AliyunOSSUtil aliyunOSSUtil;
    @ApiOperation("场馆入驻申请")
    @PostMapping("/registryGym.gym")
    public Result<RegistryGymDto> registryGym(@RequestBody @Valid RegistryGymForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int gymId = Integer.parseInt(JwtUtil.parseToken(refreshToken,4).get("gymId").toString());
        form.setGymId(gymId);
        RegistryGymDto dto = tQualificationService.registryGym(form);
        return Result.getResult(RegistryGymDto.class).setMsg("发送申请成功").setData(dto);
    }

    @ApiOperation("场馆取消入驻申请")
    @PostMapping("/cancelRegistryGym.gym")
    public Result<TQualification> cancelRegistryGym(Integer certifyId) throws Exception {
        TQualification qualification = tQualificationService.cancelRegistryGym(certifyId);
        return Result.getResult(TQualification.class).setMsg("发送申请成功").setData(qualification);
    }
    @ApiOperation("管理员审核场馆入驻申请")
    @PostMapping("/adminApproveGym.admin")
    public Result<TQualification> adminApproveGym(AdminApproveGymForm form){
        TQualification qualification = tQualificationService.adminApproveGym(form);
        return Result.getResult(TQualification.class).setMsg("审核成功").setData(qualification);
    }
    @ApiOperation("驳回后修改场馆入驻申请")
    @PostMapping("/modifyRegistryGym.gym")
    public Result<TQualification> modifyRegistryGym(@RequestBody @Valid ModifyRegistryGymForm form) throws Exception {
        TQualification qualification = tQualificationService.modifyRegistryGym(form);
        return Result.getResult(TQualification.class).setMsg("修改成功").setData(qualification);
    }

    @ApiOperation("管理员查询全部申请入驻信息")
    @GetMapping("/adminFindAllApply.admin")
    public Result adminFindAllApply(AdminFindAllApplyForm form){
        PageInfo<QualificationDto> dto = tQualificationService.adminFindAllApply(form);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setData(dto);
    }
    @ApiOperation("场馆查询自己的申请入驻信息")
    @GetMapping("/gymFindApplyById.gym")
    public Result gymFindApplyById(GymFindApplyByIdForm form,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        int gymId = Integer.parseInt(JwtUtil.parseToken(refreshToken,4).get("gymId").toString());
        form.setGymId(gymId);
        PageInfo<QualificationDto> dto = tQualificationService.gymFindApplyById(form);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setData(dto);
    }

    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public Result uploadBookImage(MultipartFile image)throws Exception{
        InputStream imageInputStream = image.getInputStream();
        byte[] header = new byte[4];
        imageInputStream.read(header);
        if(!aliyunOSSUtil.isPic(header)){
            throw new Exception("文件类型上传错误");
        }

        String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
        String name = UUID.randomUUID().toString();
        String path = aliyunOSSUtil.uploadFile(image.getInputStream(), name + suffix, "xiaowoniu107");

        return Result.getResult(String.class).setMsg("上传成功").setData(path);
    }


}

